package com.greateamd.timetableasy.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

@SuppressWarnings("serial")
@Entity
@Table(name="courses")
public class Course extends LightEntity implements PersistableEntity,HasEventsEntity,Serializable{
	
	@Id
	@GeneratedValue
	private Integer id;
	private String name;
	private String type;
	
	@Column(name="start_date",nullable=false)
	private Date startDate;
	@Column(name="end_date",nullable=false)
	private Date endDate;
	
	@Column(name="enabled",nullable=false)
	private boolean enabled = true;

	@ManyToOne
	@JoinColumn(name="promotion_id")
	private Promotion promotion;
	
	@ManyToOne
	@JoinColumn(name="study_period")
	private StudyPeriod studyPeriod;
	
	@ManyToMany(fetch = FetchType.EAGER,targetEntity=Event.class,cascade=CascadeType.ALL)
    @JoinTable(name="courses_events",
		        joinColumns = @JoinColumn(columnDefinition="course"),
		        inverseJoinColumns=@JoinColumn(name="event_id"))
	private Set<Event> events;
	
	public Course() {
	}
	
	public Course(String name,String type,Date start,Date end,StudyPeriod studyPeriod){
		this.name = name;
		this.type = type;
		this.startDate = start;
		this.endDate = end;
		this.studyPeriod = studyPeriod;
	}

	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}


	public void setId(Integer id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public StudyPeriod getStudyPeriod() {
		return studyPeriod;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setStudyPeriod(StudyPeriod studyPeriod) {
		this.studyPeriod = studyPeriod;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	@Override
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public Date getStartDate() {
		return startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public Set<Event> getEvents() {
		return events;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setEvents(Set<Event> events) {
		this.events = events;
	}

	public Promotion getPromotion() {
		return promotion;
	}

	public void setPromotion(Promotion promotion) {
		this.promotion = promotion;
	}
	
	public void addEvent(Event event) {
		if (events == null)
			events = new HashSet<Event>();
		this.events.add(event);
	}
	
	@Override
	public String toString() {
		return getName() + " " + getType();
	}
}
